package rpc.socket;

import lombok.extern.slf4j.Slf4j;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Try on 2017/6/3.
 */
@Slf4j
public class Service {

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(5200);
        while (true) {
            Socket socket = server.accept();

            // 读取服务信息
            ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
            // 接口名称
            String interfacename = input.readUTF();
            // 方法名称
            String methodName = input.readUTF();
            // 参数类型
            Class<?>[] parameterTypes = (Class<?>[]) input.readObject();
            // 参数对象
            Object[] arguments = (Object[]) input.readObject();

           log.info("读取客户端数据完毕.");

            // 执行调用
            Class serviceinterfaceclass = Class.forName(interfacename);
            Object service = getObject(interfacename);
            Method method = serviceinterfaceclass.getMethod(methodName, parameterTypes);
            Object result = method.invoke(service, arguments);
            ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
            output.writeObject(result);

            log.info("服务端处理完成.");
        }
    }

    /**
     * 给定接口名返回接口类实现的对象
     * @param interfacename 接口名
     * @return
     */
    public static Object getObject(String interfacename) throws NoSuchMethodException, InvocationTargetException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        String name = interfacename+"Impl";
        final Class<?> clazz = Class.forName(name);
        return  clazz.getDeclaredConstructor().newInstance();
    }
}
